import { SUCCESS_CODE } from '@/utils/typings.d';
import { request } from 'umi';
import type { ModifyPassword, UpdateSelfType } from './typings.d';

export async function updateSelfInfo(id: string, newInfo: UpdateSelfType) {
  if (!id || !newInfo || Object.keys(newInfo).length === 0) {
    return { success: false, message: '更新失败，请填入需要更新的信息' };
  }
  const res = await request(`/api/system-user/v1.0.0/self/${id}`, {
    method: 'PUT',
    data: newInfo,
  });
  if (res?.code === SUCCESS_CODE) return { success: true, data: res.data, message: '更新成功' };
  return { success: false, data: res?.data, message: res?.message || '更新失败' };
}
export async function updatePassword(id: string, newInfo: ModifyPassword) {
  if (!id || !newInfo || Object.keys(newInfo).length !== 2) {
    return { success: false, message: '更改失败，请填入需要的密码信息' };
  }
  const res = await request(`/api/system-user/v1.0.0/self-password/${id}`, {
    method: 'PUT',
    data: newInfo,
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, data: res.data, message: '更改成功，请重新登录' };
  return { success: false, data: res?.data, message: res?.message || '更改失败' };
}
